<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">欢迎登录书城</h2>
      <el-form :model="form" :rules="rules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password prefix-icon="Lock" />
        </el-form-item>

        <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
          立即登录
        </el-button>

        <div class="links">
          <el-link type="info" :underline="false">忘记密码?</el-link>
          <router-link to="/register"><el-link type="primary" :underline="false">立即注册</el-link></router-link>

        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import useToken from '@/store/token'
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{6,16}$/, message: '6-16位字母数字组合', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        // 调用封装好的登录接口
        const { code, data, message } = await login(this.form)
        if (code === 200) {
          console.log('登录成功',data);
          // data就是jwt
          const jwt = data
          const {updateToken} = useToken()
          // 存储token到pinia中
          updateToken(jwt)
          
          this.$message.success("登陆成功")
          this.$router.push('/')
        } else {
          this.$message.error(message || '登录失败')
          console.log('登录失败',data);
          
        }
      } catch (error) {
        console.log('登录失败', error)
        this.$message.error('登录失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #b8c5ff 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  color: #303133;
  margin-bottom: 30px;
  font-size: 24px;
  letter-spacing: 2px;
}

.login-form {
  .el-input__inner {
    height: 48px;
    line-height: 48px;
  }

  .el-input__prefix {
    display: flex;
    align-items: center;
    left: 15px;
  }
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  letter-spacing: 4px;
  margin-top: 10px;
  transition: all 0.3s;
}

.links {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.el-link+.el-link {
  margin-left: 10px;
}
</style>